﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Librerias;
using System.DirectoryServices;
using System.Data;

namespace Aplicacion.admin.procesos
{
    public partial class _default : System.Web.UI.Page
    {
     
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            
            

            int counter = 0;
            string txt = TextBox1.Text;
            string[] lista = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            TextBox1.Text = String.Empty;
            foreach (var userName in lista)
            {
                var ad = new ObjetoAd();
                string[] usuarioDNI = userName.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var src = ad.Buscar(usuarioDNI[0].Trim());
                // Si encuentra mas de un usuario...
                
                if (src != null && src.Count >= 1)
                {
                    var i = 0;
                    foreach (SearchResult sr in src)
                    {
                        counter++;
                        ad.GetObjeto(sr.Path);
                        i++;
                        //listEncontrados.Items.Add()
                    }
                }

                

            }

            Literal1.Text = "Buscando: " + lista.Count() + " - Cantidad encontrados: " + counter;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int contador = 0;
            string txt = TextBox1.Text;
            TextBox1.Text = String.Empty;
            string[] lista = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            
            foreach (var userName in lista)
            {
                var ad = new ObjetoAd();
                var src = ad.Buscar(userName.Trim());
                if (src != null && src.Count >= 1)
                {
                    var i = 0;
                    foreach (SearchResult sr in src)
                    {
                        ad.GetObjeto(sr.Path);
                        ad.Deshabilitar();
                        ad.Guardar();
                        ad.Close();
                        contador++;
                    }
                }
                
            }

            Literal1.Text = "Se modificaron " + contador + " registros";

            
        }

        protected void btnEnable_Click(object sender, EventArgs e)
        {/*
            foreach (ListEditItem item in listEncontrados.SelectedItems)
            {
                string value = item.Value.ToString();
                ObjetoAd ad = new ObjetoAd(value);
                ad.Habilitar();
                ad.Guardar();
                ad.Close();
            }*/

            int contador = 0;
            string txt = TextBox1.Text;
            TextBox1.Text = String.Empty;
            string[] lista = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var userName in lista)
            {
                var ad = new ObjetoAd();
                var src = ad.Buscar(userName.Trim());
                if (src != null && src.Count >= 1)
                {
                    var i = 0;
                    foreach (SearchResult sr in src)
                    {
                        ad.GetObjeto(sr.Path);
                        ad.Habilitar();
                        ad.Guardar();
                        ad.Close();
                        contador++;
                    }
                }

            }

            Literal1.Text = "Se modificaron " + contador + " registros";
        }

        protected void btnDNI_Click(object sender, EventArgs e)
        {
            int contador = 0;
            string txt = TextBox1.Text;
            string[] lista = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            TextBox1.Text = String.Empty;
            foreach (var userName in lista)
            {
                var ad = new ObjetoAd();
                string[] usuarioDNI = userName.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var src = ad.Buscar(usuarioDNI[0].Trim());
                // Si encuentra mas de un usuario...

                if (src != null && src.Count >= 1)
                {
                    var i = 0;
                    foreach (SearchResult sr in src)
                    {
                        contador++;
                        ad.GetObjeto(sr.Path);
                        ad.DNI = usuarioDNI[1];
                        //listEncontrados.Items.Add()
                    }
                }



            }
            
                        
            //int[] seleccionados = listEncontrados.GetSelectedIndices();
            Literal1.Text = "Se modificaron " + contador + " registros";

            
        }

        protected void btnInactivos_Click(object sender, EventArgs e)
        {

        }
    }
}